<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
    require_once('utils.inc');
	require_once('server_validations.inc');
			
	session_start();
	check_permissions();
	checkIsLoggedIn();
	
	$db = db_connect();
	
	if (isset($_POST['save'])) {
		
		$nume = $_POST['nume'];
		$prenume = $_POST['prenume'];
		$email = $_POST['email'];
		$language = $_POST['lang'];
		
		$moneda = $_POST['moneda_implicita'];
		$comanda_interna_externa = $_POST['comanda_interna_externa'];
		
        $tooltip = 0;
		if (isset($_POST['tooltip'])) 
            $tooltip = $_POST['tooltip'];

        $extra_text = 0;
        if (isset($_POST['extra_text'])) 
            $extra_text = $_POST['extra_text'];
            
        $comenzi_per_pag = $_POST['comenzi_per_pag'];
        $firme_per_pag = $_POST['firme_per_pag'];
        
        $comenzi_plata_per_pag = $_POST['comenzi_plata_per_pag'];
        $comenzi_incasat_per_pag = $_POST['comenzi_incasat_per_pag'];  

        $comenzi_incarcari_per_pag = $_POST['comenzi_incarcari_per_pag'];
        $comenzi_descarcari_per_pag = $_POST['comenzi_descarcari_per_pag']; 
		
		//server side validations
        $_SESSION['wrong_ids'] = array();
        
        if (!validPrintable($nume, 1, CT_MAX_NUME_LENGTH)) {
            $_SESSION['wrong_ids']['nume'] = 1;
        }
        $_SESSION['user_logat']->nume = $nume;
            

        if (!validPrintable($prenume, 1, CT_MAX_PRENUME_LENGTH)) {
            $_SESSION['wrong_ids']['prenume'] = 1;
        }
        $_SESSION['user_logat']->prenume = $prenume;
            
		if (!validMail($email, 1, CT_MAX_MAIL_LENGTH)) {
            $_SESSION['wrong_ids']['email'] = 1;
        }
        $_SESSION['user_logat']->email = $email;
            
            
        if ($tooltip != 0 && $tooltip != 1)
            $_SESSION['wrong_ids']['tooltip'] = 1;
            
        $_SESSION['user_logat']->tooltip = $tooltip;
        $_SESSION['user_logat']->extra_text = $extra_text;
		
        
        if (!validPrintable($language, 1, 20)) {
            $_SESSION['wrong_ids']['language'] = 1;
        }
        $_SESSION['user_logat']->language = $language;
            
            
        $aux = filter_var($comenzi_per_pag, FILTER_VALIDATE_INT);
        if ($aux === false)
            $_SESSION['wrong_ids']['comenzi_per_pag'] = 1;
        $_SESSION['user_logat']->comenzi_per_pag = $comenzi_per_pag;
                    
                    
		$aux = filter_var($firme_per_pag, FILTER_VALIDATE_INT);
        if ($aux === false)
            $_SESSION['wrong_ids']['firme_per_pag'] = 1;

        $_SESSION['user_logat']->firme_per_pag = $firme_per_pag;
                
        $_SESSION['user_logat']->moneda = $moneda;
		$_SESSION['user_logat']->comanda_interna_externa = $comanda_interna_externa;
        
		$aux = filter_var($comenzi_plata_per_pag, FILTER_VALIDATE_INT);
        if ($aux === false)
            $_SESSION['wrong_ids']['comenzi_plata_per_pag'] = 1;
        $_SESSION['user_logat']->comenzi_plata_per_pag = $comenzi_plata_per_pag;

		$aux = filter_var($comenzi_incasat_per_pag, FILTER_VALIDATE_INT);
        if ($aux === false)
            $_SESSION['wrong_ids']['comenzi_incasat_per_pag'] = 1;
        $_SESSION['user_logat']->comenzi_incasat_per_pag = $comenzi_incasat_per_pag;

		$aux = filter_var($comenzi_incarcari_per_pag, FILTER_VALIDATE_INT);
        if ($aux === false)
            $_SESSION['wrong_ids']['comenzi_incarcari_per_pag'] = 1;
        $_SESSION['user_logat']->comenzi_incarcari_per_pag = $comenzi_incarcari_per_pag;
        
		$aux = filter_var($comenzi_descarcari_per_pag, FILTER_VALIDATE_INT);
        if ($aux === false)
            $_SESSION['wrong_ids']['comenzi_descarcari_per_pag'] = 1;
        $_SESSION['user_logat']->comenzi_descarcari_per_pag = $comenzi_descarcari_per_pag;
                        
		try {
			$_SESSION['user_logat']->queryUpdate($db);			
			$_SESSION['language'] = $limbi[$_SESSION['user_logat']->language];
			notice_msg(LANG_Profilul_a_fost_modificata_cu_succes, 'home.php');
		}
        catch (QueryDuplicateEmailUserException $e) {
            $_SESSION['wrong_ids']['email'] = 1;
            $mesaj_aditional = LANG_Emailul_trebuie_sa_fie_unic;
        }
        catch (QueryUpdateUserException $e) {
            redirectOnError(CT_USER_PROFILE_UPDATE);
        }
        catch (QueryWrongVersionException $e) {
            notice_msg(LANG_Datele_pe_care_lucrati_au_fost_modificate, 'user_profile.php');
        }
        
        if (count($_SESSION['wrong_ids']) > 0) {
            notice_msg($mesaj_aditional . LANG_Campurile_rosii_trebuiesc_completate_corect, 'user_profile.php');            
        }
	}
    else {
        if (isset($_SESSION['wrong_ids'])) {
            if (count($_SESSION['wrong_ids']) > 0) {
                $wrong_ids = $_SESSION['wrong_ids'];                
                unset($_SESSION['wrong_ids']);
            }
        }    
    }
	
    try {
        $user = User::queryLoad($db, $_SESSION['user_logat']->id_user);        
    }
    catch (QueryLoadUserException $e) {
        redirectOnError(CT_USER_PROFILE_LOAD_USER);
    }
    catch (QueryLoadAddressException $e) {
        redirectOnError(CT_USER_PROFILE_LOAD_ADDRESS);
    }
    
    html_header("LANG_Setari_user", null, "user_profile");
	
	$linkuri = array(LANG_Home => "home.php", LANG_User_Profile => "");	
	titlu_pagina($linkuri);
    output_error_msg();
    output_notice_msg();
    output_extra_text(LANG_EXTRA_USER_PROFILE);

	$id_client = $_SESSION['user_logat']->id_client;
?>
	<script type="text/javascript">
		function validateForm() {
			return (validatePrintable(document.user_profile.nume.value, '<?php echo LANG_nume; ?>', true, 2) &&
						validatePrintable(document.user_profile.prenume.value, '<?php echo LANG_PRENUME; ?>', true, 5) &&
						validateMail(document.user_profile.email.value, true) &&
						validatePosInteger(document.user_profile.firme_per_pag.value, '<?php echo LANG_Firme_per_pagina; ?>', true) &&
						validatePosInteger(document.user_profile.comenzi_per_pag.value, '<?php echo LANG_Comenzi_per_pagina   ; ?>', true));
		}
	</script>
<?php
    
	echo "<div class=\"container\" align=\"left\">";
		echo "<table align=\"left\" id=\"wrapper2\">";
			echo "<form name=\"user_profile\" method=\"post\" action=\"user_profile.php\" onsubmit=\"return (validateForm());\">";
			echo "<thead>";
				echo "<tr>";
					echo "<th colspan=\"2\">";
						echo LANG_User_Profile;
					echo "</th>";
				echo "</tr>";
			echo "</thead>";
			?>
			<tr class="capTabel">
				<td valign="top">
					<fieldset>
						<legend><?php echo LANG_Date_personale; ?></legend>
						<table>
							<tr>
								<td <?php check_field('nume'); ?>>						
									<label for="nume"><?php echo LANG_NUME; ?></label>
								</td>
								<td>
									<input type="text" value="<?php echo $_SESSION['user_logat']->nume; ?>" id="nume" name="nume" />
								</td>
							</tr>
							<tr>
								<td <?php check_field('prenume'); ?>>						
									<label for="prenume"><?php echo LANG_PRENUME; ?></label>
								</td>
								<td>
									<input type="text" value="<?php echo $_SESSION['user_logat']->prenume; ?>" id="prenume" name="prenume" />
								</td>
							</tr>						
							<tr>
								<td <?php check_field('email'); ?>>						
									<label for="email"><?php echo LANG_EMAIL; ?></label>
								</td>
								<td>
									<input type="text" value="<?php echo $_SESSION['user_logat']->email; ?>" id="email" name="email" />
								</td>
							</tr>
						</table>
					</fieldset>	
					<fieldset>
						<legend><?php echo LANG_Liste_Firme_Comenzi; ?></legend>
						<table>
							<tr>
								<td>						
									<label for="firme_per_pag"><?php echo LANG_Firme_per_pagina; ?></label>									
								</td>
								<td>
									<select name="firme_per_pag">
										<?php
											$values = array(10,20,30,40,50,70,100);
											for ($i = 0; $i < count($values); $i++) {
												if ($values[$i] == $_SESSION['user_logat']->firme_per_pag) $text = " selected=\"selected\" ";
												else $text = 1;
												echo "<option $text value=\"{$values[$i]}\">{$values[$i]}</option>";
											}
										?>
									</select>									
								</td>
							</tr>
							<tr>
								<td>						
									<label for="comenzi_per_pag"><?php echo LANG_Comenzi_per_pagina ?></label>									
								</td>
								<td>
									<select name="comenzi_per_pag">
										<?php
											
											for ($i = 0; $i < count($values); $i++) {
												if ($values[$i] == $_SESSION['user_logat']->comenzi_per_pag) $text = " selected=\"selected\" ";
												else $text = 1;
												echo "<option $text value=\"{$values[$i]}\">{$values[$i]}</option>";
											}
										?>
									</select>									
								</td>
							</tr>						
							<tr>
								<td>						
									<label for="comenzi_plata_per_pag"><?php echo LANG_Comenzi_ptr_plata_per_pagina ?></label>									
								</td>
								<td>
									<select name="comenzi_plata_per_pag">
										<?php
											
											for ($i = 0; $i < count($values); $i++) {
												if ($values[$i] == $_SESSION['user_logat']->comenzi_plata_per_pag) $text = " selected=\"selected\" ";
												else $text = 1;
												echo "<option $text value=\"{$values[$i]}\">{$values[$i]}</option>";
											}
										?>
									</select>									
								</td>
							</tr>	
							<tr>
								<td>						
									<label for="comenzi_incasat_per_pag"><?php echo LANG_Comenzi_ptr_incasat_per_pagina ?></label>									
								</td>
								<td>
									<select name="comenzi_incasat_per_pag">
										<?php
											
											for ($i = 0; $i < count($values); $i++) {
												if ($values[$i] == $_SESSION['user_logat']->comenzi_incasat_per_pag) $text = " selected=\"selected\" ";
												else $text = 1;
												echo "<option $text value=\"{$values[$i]}\">{$values[$i]}</option>";
											}
										?>
									</select>									
								</td>
							</tr>	
							<tr>
								<td>						
									<label for="comenzi_incarcari_per_pag"><?php echo LANG_Comenzi_ptr_incarcari_per_pagina ?></label>									
								</td>
								<td>
									<select name="comenzi_incarcari_per_pag">
										<?php
											
											for ($i = 0; $i < count($values); $i++) {
												if ($values[$i] == $_SESSION['user_logat']->comenzi_incarcari_per_pag) $text = " selected=\"selected\" ";
												else $text = 1;
												echo "<option $text value=\"{$values[$i]}\">{$values[$i]}</option>";
											}
										?>
									</select>									
								</td>
							</tr>		
							<tr>
								<td>						
									<label for="comenzi_descarcari_per_pag"><?php echo LANG_Comenzi_ptr_descarcari_per_pagina ?></label>									
								</td>
								<td>
									<select name="comenzi_descarcari_per_pag">
										<?php
											
											for ($i = 0; $i < count($values); $i++) {
												if ($values[$i] == $_SESSION['user_logat']->comenzi_descarcari_per_pag) $text = " selected=\"selected\" ";
												else $text = 1;
												echo "<option $text value=\"{$values[$i]}\">{$values[$i]}</option>";
											}
										?>
									</select>									
								</td>
							</tr>																																		
						</table>
					</fieldset>						
				</td>
				<td valign="top">
					<fieldset>
						<legend><?php echo LANG_Optiuni_Generale; ?></legend>
						<table>
							<tr>
								<td <?php check_field('tooltip'); ?>
									<label for="tooltip"><?php echo LANG_Show_tooltip; ?></label>
								</td>
								<td>
									<input type="checkbox"<?php if ($_SESSION['user_logat']->tooltip == 1) echo "checked=\"checked\""; ?> value="1" name="tooltip" />
								</td>
							</tr>
                            <tr>
                                <td <?php check_field('tooltip'); ?>
                                    <label for="tooltip"><?php echo LANG_Show_help_text; ?></label>
                                </td>
                                <td>
                                    <input type="checkbox"<?php if ($_SESSION['user_logat']->extra_text == 1) echo "checked=\"checked\""; ?> value="1" name="extra_text" />
                                </td>
                            </tr>
							<tr>
								<td <?php check_field('language'); ?>
									<label for="tooltip"><?php echo LANG_Language; ?></label>
								</td>
								<td>
									<?php 
										$limbi_i18n = getLimbi_i18n(); 
										render_combobox("lang", $limbi_int, $limbi_i18n, $_SESSION['user_logat']->language);
									?>
								</td>
							</tr>		
							<tr>
								<td><?php echo LANG_Moneda_implicita; ?></td>
								<td><?php render_combobox("moneda_implicita", array_keys($currencies), $currencies, $_SESSION['user_logat']->moneda); ?></td>
							</tr>	
							<tr>
								<td><?php echo LANG_Comanda_implicita_este; ?></td>
								<td>
									<?php 
										$comanda_int_ext = array(CT_COMANDA_INTERNA => constant("LANG_Comanda_interna"), CT_COMANDA_EXTERNA => constant("LANG_Comanda_externa"));
										render_combobox("comanda_interna_externa", array_keys($comanda_int_ext), $comanda_int_ext, $_SESSION['user_logat']->comanda_interna_externa); 
									?>
								</td>
							</tr>																		
						</table>
					</fieldset>		
                    <fieldset>
                        <legend><?php echo LANG_Campuri_Comenzi_Firme; ?></legend>
                        <table>
                            <tr>
                                <td>                        
                                    <input onclick="javascript: custom_field_firm('save', <?php echo DIM_Cap_Tabel_Arata_Firme_Latime; ?>, <?php echo DIM_Cap_Tabel_Arata_Firme_Inaltime; ?>);" type="button" name="select_firme" id="select_firme" value="<?php echo LANG_Select_Camp_Firme; ?>" />
                                </td>
                                <td>
									<input onclick="javascript: custom_field_comand('save', <?php echo DIM_Cap_Tabel_Arata_Comenzi_Latime; ?>, <?php echo DIM_Cap_Tabel_Arata_Comenzi_Inaltime; ?>)" type="button" name="select_comenzi" id="select_comenzi" value="<?php echo LANG_Select_Camp_Comenzi; ?>" />
                                </td>
                            </tr>    
                        </table>
                    </fieldset>                                									
				</td>	
			</tr>	
			<tr class="capTabel">
				<td colspan="2" align="center">
					<?php buton_final_ok(LANG_Save_Options, "save"); ?>	
					&nbsp;&nbsp;
					<?php buton_final_cancel(LANG_Cancel, "home.php"); ?>																
				</td>
			</tr>
			<?php		
			echo "</form>";
		echo "</table>";
	echo "</div>";

    unset($_SESSION['wrong_ids']);
	html_footer();
?>